package com.sort;

import java.util.Arrays;

/**
 * 选择排序算法
 * 作者：swk
 * 时间：
 * 公司：
 **/
public class SelectSort {
    public static long count = 0;

    public static void main(String[] args) {
        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 8000000);
        }
        long beginTime = System.currentTimeMillis();
        selectSort(arr);
        long endTime = System.currentTimeMillis();
        //System.out.println("排序后结果：" + Arrays.toString(arr));
        System.out.println("选择排序耗时：" + (endTime - beginTime));
        System.out.println("循环次数：" + count);
        count = 0;


    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int min = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    min = arr[j];
                    minIndex = j;
                }
                count++;
            }
            if (minIndex != i) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }

            //System.out.println("第" + (i + 1) + "轮排序结果" + Arrays.toString(arr));

        }
    }
}